<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>fetch upfile</title>
</head>

<body>
上传文件：<input type="file" name="file" accept="image/jpeg,image/png">
<button>开始上传</button>
<hr>
上传文件：<input type="file" name="filem" multiple accept="image/jpeg,image/png">
<button>开始上传多文件</button>
<script>
    let btn = document.querySelector("button")
    btn.addEventListener('click', function () {
        let data = new FormData()
        data.append("user", "李勇")
        data.append('file', document.querySelector('input[name=file]').files[0])
        //同一服务器下
        fetch("/upload", {
            method: 'post',
            body: data
        }).then(res => res.text())
            .then(res => {
                console.log(res)
            }).catch(e => {
            console.log(e)
        })
    })

    let btnm = document.querySelectorAll('button')[1]
    btnm.addEventListener('click', function () {

        let data = new FormData()
        data.append("user", "张三丰")
        let fs = document.querySelector('input[name=filem]')
        for (let file of fs.files) {
            data.append('file', file,file.name)
        }
        //同一服务器下
        fetch("/uploads", {
            method: 'post',
            body: data
        }).then(res => res.text())
            .then(res => {
                console.log(res)
            }).catch(e => {
            console.log(e)
        })
    })

</script>
</body>
</html>
